package com.scu.dao;

import com.scu.entity.mComment;
import com.scu.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class mCommentDao {

    public static int insert(mComment comment) throws SQLException {
        //1. 连接
        Connection conn = JDBCUtil.getConnection();

        //2. sql
        String sql = "INSERT INTO comments(comment_id,comment_content,user_id,dates,post_id) VALUES(?,?,?,?,?)";
        PreparedStatement prep = conn.prepareStatement(sql);
        prep.setInt(1,comment.getComment_id());
        prep.setString(2,comment.getComment_content());
        prep.setInt(3,comment.getUser_id());
        prep.setString(4,comment.getDates());
        prep.setInt(5,comment.getPost_id());



        //3. 结果
        int row = prep.executeUpdate();

        //4. 关闭连接
        conn.close();

        return row;
    }

    public static List<mComment> findSpe() throws SQLException {

        Connection conn = JDBCUtil.getConnection();

        String sql = "select * from comments";
        PreparedStatement prep = conn.prepareStatement(sql);


        ResultSet rs = prep.executeQuery();

        List<mComment> list = new ArrayList<mComment>();
        while(rs.next()){
            //rs代表的就是每一行
            mComment comment = new mComment();



            int comment_id = rs.getInt("comment_id");
            String comment_content = rs.getString("comment_content");
            int user_id = rs.getInt("user_id");
            String dates = rs.getString("dates");
            int post_ids = rs.getInt("post_id");
            comment.setComment_id(comment_id);
            comment.setComment_content(comment_content);
            comment.setUser_id(user_id);
            comment.setDates(dates);
            comment.setPost_id(post_ids);

            list.add(comment);
        }

        //4. 关闭连接
        conn.close();

        return list;
    }

    public static void main(String[] args) throws SQLException {
//        mComment comment = new mComment();
//        comment.setComment_content("测试一下测试一下");
//        comment.setPost_id(6);
//        comment.setUser_id(194433);
//        comment.setDates("202207715");
//        int insert = mCommentDao.insert(comment);
//        List<mComment> comments = mCommentDao.findSpe(5);
//        System.out.println(comments);
    }

}
